package com.aidex.system.service.impl;

import com.aidex.common.core.service.BaseServiceImpl;
import com.aidex.system.domain.CargoDetail;
import com.aidex.system.domain.RiskGoodsNameKeySuggest;
import com.aidex.system.domain.vo.SuggestApply;
import com.aidex.system.mapper.RiskGoodsNameKeySuggestMapper;
import com.aidex.system.service.CargoDetailService;
import com.aidex.system.service.RiskGoodsNameKeySuggestService;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 优化建议信息Service业务层处理
 * @author jmy
 * @email jmyinjg@163.com
 * @date 2021-10-14
 */
@Service
@Transactional(readOnly = false)
public class RiskGoodsNameKeySuggestServiceImpl extends BaseServiceImpl<RiskGoodsNameKeySuggestMapper, RiskGoodsNameKeySuggest> implements RiskGoodsNameKeySuggestService
{
    private static final Logger log = LoggerFactory.getLogger(RiskGoodsNameKeySuggestServiceImpl.class);

    @Autowired
    private CargoDetailService cargoDetailService;

    /**
     * 获取单条数据
     * @param riskGoodsNameKeySuggest 优化建议信息
     * @return 优化建议信息
     */
    @Override
    public RiskGoodsNameKeySuggest get(RiskGoodsNameKeySuggest riskGoodsNameKeySuggest)
    {
        RiskGoodsNameKeySuggest dto = super.get(riskGoodsNameKeySuggest);
        return dto;
    }

    /**
     * 获取单条数据
     * @param id 优化建议信息id
     * @return 优化建议信息
     */
    @Override
    public RiskGoodsNameKeySuggest get(String id)
    {
        RiskGoodsNameKeySuggest dto = super.get(id);
        return dto;
    }


    /**
     * 查询优化建议信息列表
     * @param riskGoodsNameKeySuggest 优化建议信息
     * @return 优化建议信息
     */
    @Override
    public List<RiskGoodsNameKeySuggest> findList(RiskGoodsNameKeySuggest riskGoodsNameKeySuggest)
    {
        return super.findList(riskGoodsNameKeySuggest);
    }

    /**
     * 分页查询优化建议信息列表
     * @param riskGoodsNameKeySuggest 优化建议信息
     * @return 优化建议信息
     */
    @Override
    public PageInfo<RiskGoodsNameKeySuggest> findPage(RiskGoodsNameKeySuggest riskGoodsNameKeySuggest)
    {
        return super.findPage(riskGoodsNameKeySuggest);
    }

    /**
     * 保存优化建议信息
     * @param riskGoodsNameKeySuggest
     * @return 结果
     */
    @Override
    public boolean save(RiskGoodsNameKeySuggest riskGoodsNameKeySuggest)
    {
        return super.save(riskGoodsNameKeySuggest);
    }


    /**
     * 删除优化建议信息信息
     * @param riskGoodsNameKeySuggest
     * @return 结果
     */
    @Override
    public boolean remove(RiskGoodsNameKeySuggest riskGoodsNameKeySuggest)
    {
        return super.remove(riskGoodsNameKeySuggest);
    }

    /**
     * 批量删除优化建议信息
     * @param ids 需要删除的优化建议信息ID
     * @return 结果
     */
    @Transactional(readOnly = false)
    @Override
    public int deleteRiskGoodsNameKeySuggestByIds(String[] ids)
    {
        return mapper.deleteRiskGoodsNameKeySuggestByIds(ids);
    }

    @Override
    @Transactional(readOnly = false)
    public Boolean apply(SuggestApply apply) {
        for (String id : apply.getIds()) {
            RiskGoodsNameKeySuggest suggest = this.get(id);
            if (0 != suggest.getStatus()) {
                continue;
            }
            suggest.setStatus(apply.getStatus());
            mapper.update(suggest);
        }
        return Boolean.TRUE;
    }

    @Override
    @Transactional(readOnly = false)
    public Boolean confirm(SuggestApply apply) {
        for (String id : apply.getIds()) {
            RiskGoodsNameKeySuggest suggest = this.get(id);
            if (1 != suggest.getStatus()) {
                continue;
            }
            suggest.setStatus(apply.getStatus());
            mapper.update(suggest);
            if (3 == suggest.getStatus()) {
                CargoDetail detail = new CargoDetail();
                detail.setKeywords(suggest.getKeywords());
                detail.setType(suggest.getType());
                detail.setSource(2);
                detail.setSpecialCode(suggest.getSpecialCode());
                detail.setStatus(1);
                cargoDetailService.save(detail);
            }
        }
        return Boolean.TRUE;
    }

    @Override
    public RiskGoodsNameKeySuggest getByName(String name) {
        return mapper.getByName(name);
    }

    @Override
    public void addSuggest(String key, Integer type) {
        RiskGoodsNameKeySuggest suggest = new RiskGoodsNameKeySuggest();
        suggest.setKeywords(key);
        suggest.setType(type);
        suggest.setStatus(0);
        this.save(suggest);
    }

}
